package com.papers.juan.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.papers.common.annotation.Log;
import com.papers.common.annotation.RepeatSubmit;
import com.papers.common.core.controller.BaseController;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.domain.R;
import com.papers.common.core.page.TableDataInfo;
import com.papers.common.core.validate.AddGroup;
import com.papers.common.core.validate.EditGroup;
import com.papers.common.enums.BusinessType;
import com.papers.common.exception.ServiceException;
import com.papers.common.utils.poi.ExcelUtil;
import com.papers.juan.domain.bo.TJuanPaperRecordBo;
import com.papers.juan.domain.vo.TJuanPaperRecordVo;
import com.papers.juan.service.ITJuanPaperRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 试卷维护记录
 *
 * @author papers
 * @date 2022-10-08
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/juan/juanPaperRecord")
public class TJuanPaperRecordController extends BaseController {

    private final ITJuanPaperRecordService iTJuanPaperRecordService;

    /**
     * 查询试卷维护记录列表
     */
    @SaCheckPermission("juan:juanPaperRecord:list")
    @GetMapping("/list")
    public TableDataInfo<TJuanPaperRecordVo> list(TJuanPaperRecordBo bo, PageQuery pageQuery) {
        return iTJuanPaperRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 根据试卷id获取试卷维护记录列表
     *
     * @param paperId   试卷id
     * @param pageQuery 分页条件
     * @return 试卷维护记录列表
     */
    @GetMapping("/queryPaperRecordListByPaperId")
    public TableDataInfo<TJuanPaperRecordVo> queryPaperRecordListByPaperId(@NotNull(message = "试卷id不能为空") String paperId, PageQuery pageQuery) {
        log.debug("根据试卷id获取试卷维护记录列表，试卷id：{}，分页条件：{}", paperId, pageQuery);
        TableDataInfo<TJuanPaperRecordVo> list = iTJuanPaperRecordService.queryPaperRecordListByPaperId(paperId, pageQuery);
        log.debug("根据试卷id获取试卷维护记录列表，出参：{}", list);
        return list;
    }

    /**
     * 导出试卷维护记录列表
     */
    @SaCheckPermission("juan:juanPaperRecord:export")
    @Log(title = "试卷维护记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TJuanPaperRecordBo bo, HttpServletResponse response) {
        List<TJuanPaperRecordVo> list = iTJuanPaperRecordService.queryPageList(bo, new PageQuery()).getRows();
        if (CollectionUtils.isEmpty(list)) {
            throw new ServiceException("维护记录列表不能为空！");
        }
        ExcelUtil.exportExcel(list, "试卷维护记录", TJuanPaperRecordVo.class, response);
    }

    /**
     * 导出组卷维护记录列表(包含"手动新增"和"自动生成")
     */
    @SaCheckPermission("juan:juanPaperRecord:export")
    @Log(title = "组卷维护记录", businessType = BusinessType.EXPORT)
    @GetMapping("/exportGeneratePaperRecords")
    public void exportGeneratePaperRecords(HttpServletResponse response) {
        List<TJuanPaperRecordVo> list = iTJuanPaperRecordService.exportGeneratePaperRecords();
        ExcelUtil.exportExcel(list, "组卷维护记录", TJuanPaperRecordVo.class, response);
    }

    /**
     * 获取试卷维护记录详细信息
     *
     * @param recordId 主键
     */
    @SaCheckPermission("juan:juanPaperRecord:query")
    @GetMapping("/{recordId}")
    public R<TJuanPaperRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                         @PathVariable String recordId) {
        return R.ok(iTJuanPaperRecordService.queryById(recordId));
    }

    /**
     * 新增试卷维护记录
     */
    @SaCheckPermission("juan:juanPaperRecord:add")
    @Log(title = "试卷维护记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TJuanPaperRecordBo bo) {
        return toAjax(iTJuanPaperRecordService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改试卷维护记录
     */
    @SaCheckPermission("juan:juanPaperRecord:edit")
    @Log(title = "试卷维护记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TJuanPaperRecordBo bo) {
        return toAjax(iTJuanPaperRecordService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除试卷维护记录
     *
     * @param recordIds 主键串
     */
    @SaCheckPermission("juan:juanPaperRecord:remove")
    @Log(title = "试卷维护记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] recordIds) {
        return toAjax(iTJuanPaperRecordService.deleteWithValidByIds(Arrays.asList(recordIds), true) ? 1 : 0);
    }
}
